package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.SneakyThrows;
import lombok.experimental.PackagePrivate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private DamageListDao damageListDao;

    //保存报损单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(DamageList damageList, String damageListGoodsStr, Map<String, Object> map) {


        Integer integer1 = damageListDao.saveDamageList(damageList);
        Integer integer2 = new Integer(0);


        List<DamageListGoods> damageListGoodsList = JSONObject.parseArray(damageListGoodsStr, DamageListGoods.class);
        for (DamageListGoods damageListGoods : damageListGoodsList) {
            damageListGoods.setDamageListId(damageList.getDamageListId());
            integer2= damageListGoodsDao.saveDamageListGoods(damageListGoods);
        }

        if (integer1 > 0 && integer2 > 0) {
            return true;
        }
        return false;
    }

    //报损单查询
    @SneakyThrows
    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        Map<String, Object> hashMap = new HashMap<>();

        List<DamageList> damageListList= damageListDao.list(sTime,eTime);

        hashMap.put("rows",damageListList);
        return hashMap;
    }

    //查询报损单商品信息
    @Override
    public Map<String, Object> goodList(Integer damageListId) {
        Map<String, Object> map = new HashMap<>();
        List<DamageListGoods>  damageListGoodsList=damageListDao.goodList(damageListId);
        map.put("rows", damageListGoodsList);

        return map;
    }

}
